//
//  ReadViewController.m
//  Leisure
//
//  Created by xalo on 16/6/28.
//  Copyright © 2016年 Eason. All rights reserved.
//

#import "ReadViewController.h"

@interface ReadViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong)UICollectionView *collectionView;
//阅读模型数组
@property (nonatomic,strong)NSMutableArray *dataSource;
//轮播图数组
@property (nonatomic,strong)NSMutableArray *scrollArray;

@end

@implementation ReadViewController
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (NSMutableArray *)scrollArray{
    if (!_scrollArray) {
        _scrollArray = [NSMutableArray array];
    }
    return _scrollArray;
}

-(UICollectionView *)collectionView{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//        设置item大小
        layout.itemSize = CGSizeMake(100, 100);
//        设置间距
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 20, 10);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, kHeight/3+64, kWidth, kHeight/2) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;

//    self.view.backgroundColor = [UIColor redColor];
    
//    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"celled"];
//注册XIB
    [self.collectionView registerNib:[UINib nibWithNibName:@"ReadCell" bundle:nil] forCellWithReuseIdentifier:@"ReadCell"];
        [self.view addSubview:self.collectionView];
    
    [RequestManager fectDataWithUrl:kReadUrl parameter:@{} method:@"GET" compelet:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *dic2  in dic[@"data"][@"carousel"] ) {
//            添加轮播图的数据源
            [self.scrollArray addObject:dic2[@"img"]];
        }
//       添加collectionView的数据源
        for (NSDictionary *dic3 in dic[@"data"][@"list"]) {
            ReadModel *model = [[ReadModel alloc]init];
            [model setValuesForKeysWithDictionary:dic3];
            [self.dataSource addObject:model];
        }
        
    } upDateUI:^{
        //    创建轮播图
        ZCScroView *scroView = [[ZCScroView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight/3) array:self.scrollArray];
        [self.view addSubview:scroView];
        
        [self.collectionView reloadData];
    }];
    
    
    
}



#pragma mark -------------------collectionViewDelegate----------------------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ReadCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ReadCell" forIndexPath:indexPath];
    [cell setValuesWithModel:self.dataSource[indexPath.row]];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
//    通过Xib来获取控制器
    ReadListViewController *readList = [[ReadListViewController alloc]initWithNibName:@"ReadListViewController" bundle:nil];
    
//    这个方法只能获取view，不能获取控制器，像底下这种获取控制器的方式是错误的
//    ReadListViewController *readList = [[NSBundle mainBundle]loadNibNamed:@"ReadListViewController" owner:nil options:nil].lastObject;
    
    ReadModel *model = self.dataSource[indexPath.row];
    readList.typeID = model.type;
    
    
    [self.navigationController pushViewController:readList animated:YES];
}




@end
